/**
 * file:   main_loop_stm32_raw.c
 * author: li xianjing <xianjimli@hotmail.com>
 * brief:  main loop for stm32
 *
 * copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd. 
 *
 * this program is distributed in the hope that it will be useful,
 * but without any warranty; without even the implied warranty of
 * merchantability or fitness for a particular purpose.  see the
 * license file for more details.
 *
 */

/**
 * history:
 * ================================================================
 * 2018-05-11 li xianjing <xianjimli@hotmail.com> created
 *
 */
#include "gd32f4xx.h"
#include "stdbool.h"
#include "base/g2d.h"
#include "base/idle.h"
#include "base/timer.h"
#include "lcd/lcd_mem.h"
#include "tkc/mem.h"
#include "base/lcd.h"
#include "lcd/lcd_mem_bgr565.h"
#include "lcd/lcd_mem_fragment.h"
#include "main_loop/main_loop_simple.h"
#include "stdio.h"
#include "math.h"
#include "xpt2046_drv.h"

/*LCD缓存指针在此处被定义*/
extern uint16_t display_memory_front[];

//extern __ALIGNED(16) uint16_t display_memory_back[];

extern bool g_touchevent;
extern uint32_t sysCurrentTime;


#if USE_LCD_SYNC
volatile bool lcd_sync_flag = false;

extern void layerSetNextBuf(uint32_t bufaddr);
static lcd_begin_frame_t org_begin_frame;
void TLI_IRQHandler(void)
{
		FlagStatus status;
		status = tli_interrupt_flag_get(TLI_INT_FLAG_LCR);
		tli_interrupt_flag_clear(TLI_INT_FLAG_LCR);
		
		lcd_sync_flag = true;
}

static lcd_begin_frame_t org_begin_frame;
ret_t awtk_lcd_begin_frame(lcd_t* lcd, const dirty_rects_t* dirty_rects)
{
		while(lcd_sync_flag==FALSE);
		lcd_sync_flag = false;
		return org_begin_frame(lcd, dirty_rects);
}

ret_t customLcdSync(lcd_t* lcd)
{		
		layerSetNextBuf((uint32_t)&display_memory_front[0]);
		return 0;
}

#endif
touchPoint_t touchPoint;
//touchPoint_t touchPointTest[2] = {0};
uint8_t platform_disaptch_input(main_loop_t* loop) {
	
	static uint32_t last_time = 0;
	static bool frist = true;
	static bool touched = false;
	static touchPoint_t lastTouchPoint;
	uint32_t current_time = 0;
	current_time = sysCurrentTime;
  static int count = 0;
	FlagStatus inputStatus = RESET;
	float twoPoindDistance = 0.0f;
	inputStatus = gpio_input_bit_get(GPIOD,GPIO_PIN_7);
	
	if((current_time-last_time)>30)
	{
		if(inputStatus==RESET)
		{
				touchPoint = GetCurrentTouchPoint();
				printf("%d,%d\r\n",touchPoint.x,touchPoint.y);
				if(frist == true)
				{
						lastTouchPoint = touchPoint;
						frist = false;
						touched = true;
						main_loop_post_pointer_event(loop, TRUE,touchPoint.x,touchPoint.y);
				}else{
						frist = false;
						touched = true;
						twoPoindDistance = sqrtf((touchPoint.x-lastTouchPoint.x)*(touchPoint.x-lastTouchPoint.x)+(touchPoint.y-lastTouchPoint.y)*(touchPoint.y-lastTouchPoint.y));
						if(twoPoindDistance>20.0f)
						{
								touchPoint = lastTouchPoint;
								main_loop_post_pointer_event(loop, TRUE,lastTouchPoint.x,lastTouchPoint.y);
								
						}else{
								main_loop_post_pointer_event(loop, TRUE,touchPoint.x,touchPoint.y);							
						}
				}
				lastTouchPoint = touchPoint;
				
		}else{
				if(touched == true)
				{
						frist = true;
						touched = false;
						main_loop_post_pointer_event(loop, FALSE, lastTouchPoint.x, lastTouchPoint.y);
				}else{
						lastTouchPoint.x = 0;
						lastTouchPoint.y = 0;
						main_loop_post_pointer_event(loop, FALSE, lastTouchPoint.x, lastTouchPoint.y);
				}
		}
			last_time = current_time;
	}
	return 0;
}

static ret_t main_loop_dispatch(main_loop_simple_t* loop) {
	 
	return RET_OK;
}

lcd_t* platform_create_lcd(wh_t w, wh_t h) {
		lcd_t* lcd = NULL;
		lcd = lcd_mem_bgr565_create_single_fb(w, h,(uint8_t*)&display_memory_front[0]);//开辟单缓存
#if USE_LCD_SYNC		
		org_begin_frame = lcd->begin_frame;
		lcd->begin_frame = awtk_lcd_begin_frame;
		lcd->sync = customLcdSync;
#endif
		return lcd;
}
#include "main_loop/main_loop_raw.inc"